package com.nanyang.tms.api.dto.request.saleprice.add;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.nanyang.common.core.dict.CommonEnum;
import com.nanyang.common.core.domain.ApiRequest;
import com.nanyang.common.core.exception.ValidateException;
import lombok.Data;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.List;

@Data
public class SaleBaseAddRequest extends ApiRequest {
    @JsonIgnore
    private String pin;
    @Valid
    @NotNull(message = "salePriceAddRequest不能为空")
    private SalePriceAddRequest salePriceAddRequest;
    @Valid
    @NotNull(message = "salePartSchemeAddRequest不能为空")
    private SalePartSchemeAddRequest salePartSchemeAddRequest;
    @Valid
    @NotNull(message = "saleWeightAddRequest不能为空")
    private SaleWeightAddRequest saleWeightAddRequest;
    @Valid
    @NotNull(message = "salePriceRecordAddRequestList不能为空")
    private List<SalePriceRecordAddRequest> salePriceRecordAddRequestList;
    @Override
    public void requestCheck() {
        List<SaleWeightRecordAddRequest> saleWeightRecordAddRequestList = saleWeightAddRequest.getSaleWeightRecordAddRequestList();
        BigDecimal[][] weightArray= new BigDecimal[saleWeightRecordAddRequestList.size()][2];
        for (int i = 0; i < saleWeightRecordAddRequestList.size(); i++) {
            weightArray[i][0] = saleWeightRecordAddRequestList.get(i).getWeightStart();
            weightArray[i][1] = saleWeightRecordAddRequestList.get(i).getWeightEnd();
            String chargingName = saleWeightRecordAddRequestList.get(i).getChargingName();
            if("续单价".equals(chargingName)){
                if(i==0){
                    throw new ValidateException(CommonEnum.ARG_NOTVALID.getCode(), "重量模板明细 续单价之前必须是总价！");
                }
                String frontCharging = saleWeightRecordAddRequestList.get(i-1).getChargingName();
                if(!"总价".equals(frontCharging)){
                    throw new ValidateException(CommonEnum.ARG_NOTVALID.getCode(), "重量模板明细 续单价之前必须是总价！");
                }
            }
            if("续阶单价".equals(chargingName)){
                if(i==0){
                    throw new ValidateException(CommonEnum.ARG_NOTVALID.getCode(), "重量模板明细 续阶单价之前必须是总价或续阶单价！");
                }
                String frontCharging = saleWeightRecordAddRequestList.get(i-1).getChargingName();
                if("单价".equals(frontCharging)||"续单价".equals(frontCharging)){
                    throw new ValidateException(CommonEnum.ARG_NOTVALID.getCode(), "重量模板明细 续阶单价之前必须是总价或续阶单价！");
                }
            }
        }
        checkArray(weightArray);

    }

    private void checkArray(BigDecimal[][] array){
        if (0!=array[0][0].doubleValue()) {
            throw new ValidateException(CommonEnum.ARG_NOTVALID.getCode(), "不是以0开头，该区间为不连续区间");
        }
        for (int k = 0; k < array.length - 1; k++) {
            if (array[k][1].compareTo(array[k + 1][0])!=0) {
                throw new ValidateException(CommonEnum.ARG_NOTVALID.getCode(),"该区间为不连续区间");
            }
        }
    }
}
